package net.huashitong.Project.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import net.huashitong.Project.entity.NodeNewfile;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by hyp1202 on 2018/7/12.
 */
@Repository
public class NodeNewFilterDao extends StringDao<NodeNewfile>{

    public List<NodeNewfile> queryNodeNewfile(NodeNewfile nodeNewfile){
        GenericQuery query = createQuery("obj");
        if (nodeNewfile.getNodeId()!=null&&!"".equals(nodeNewfile.getNodeId()))
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId",nodeNewfile.getNodeId());
        if (nodeNewfile.getNodeName()!=null&&!"".equals(nodeNewfile.getNodeName()))
            query.append(" and obj.nodeName like:nodeName").setParam("nodeName","%"+nodeNewfile.getNodeName()+"%");
        if (nodeNewfile.getResDept()!=null&&!"".equals(nodeNewfile.getResDept()))
            query.append(" and obj.resDept =:resDept").setParam("resDept",nodeNewfile.getResDept());
        if (nodeNewfile.getFileName()!=null&&!"".equals(nodeNewfile.getFileName()))
            query.append(" and obj.fileName like:fileName").setParam("fileName","%"+nodeNewfile.getFileName()+"%");
        if (nodeNewfile.getStatus()!=null&&!"".equals(nodeNewfile.getStatus()))
            query.append(" and obj.status =:status").setParam("status",nodeNewfile.getStatus());
        return  query.listResult();
    }

    public List<NodeNewfile> queryNodeName(){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT node_name nodeName FROM t_bnp_node_newfile GROUP BY node_name ORDER BY node_id+0");
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(NodeNewfile.class));
        List<NodeNewfile> dtos = (List<NodeNewfile>) query.list();
        return dtos;
    }

    public List<NodeNewfile> getFilesByNodeId(String nodeId){
        GenericQuery query = createQuery("obj");
        query.append(" and obj.nodeId =:nodeId").setParam("nodeId", nodeId);
        return query.listResult();
    }

}
